/*
 * ArchiveEntry.java
 *
 * Created on January 9, 2008, 8:20 PM
 */

package picto.io;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;

/**
 * 
 * @author davedes
 */
public class ArchiveEntry extends ZipEntry {
    
    public static final String XML_ENTRY_NAME = "project.xml";
    
    private static final Provider DEFAULT_PROVIDER = new Provider() {
        public InputStream createInputStream() { 
            return new ByteArrayInputStream("".getBytes()); 
        }
    };
    
    private Provider provider;
    
    public ArchiveEntry(String name) {
        this(name, null);
    }
    
    public ArchiveEntry(String name, Provider provider) {
        super(name);
        this.provider = provider;
    }
    
    public ArchiveEntry(ZipEntry entry) {
        super(entry);
    }

    /**
     * Attempts to open a stream with the attached provider. If no provider
     * is found, an empty InputStream is returned.
     * 
     * @return the input stream by the Provider or a default, empty stream if
     *          no providers exist
     */
    public InputStream openStream() {
        return provider!=null 
                ? provider.createInputStream()
                : DEFAULT_PROVIDER.createInputStream();
    }
    
    public Provider getInputProvider() {
        return provider;
    }

    public void setInputProvider(Provider provider) {
        this.provider = provider;
    }
    
    public static interface Provider {
        public InputStream createInputStream();
    }
}
